//
//  DistributionViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/27.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "DistributionViewModel.h"
#import "DistributionTableViewCell.h"

@interface DistributionViewModel ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation DistributionViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [self creteHeaderView];
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(66);
        [superView addSubview:tableView];
    }
    return self;
}

- (UIView *)creteHeaderView {
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DistributionTableViewCell * cell = [DistributionTableViewCell cellWithTableView:tableView];
    FillExpressModel *model = self.dataArr[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for (int i = 0; i < self.dataArr.count; i ++) {
        FillExpressModel *model = self.dataArr[i];
        if (i == indexPath.row) {
            if (model.isShow) {
                model.isShow = NO;
            } else {
                model.isShow = YES;
            }
        } else {
            model.isShow = NO;
        }
    }
    [tableView reloadData];

    if (self.BlockSelectRow) {
        FillExpressModel *expressModel = self.dataArr[indexPath.row];
        self.BlockSelectRow(expressModel);
    }
}

- (NSMutableArray *)makeDistributionData
{
    FillExpressModel *model1 = [[FillExpressModel alloc] init];
    model1.topStr = @"卖家承运";
    model1.detailStr = @"由卖方发货，并提供售后服务";
    model1.isShow = NO;
    
    FillExpressModel *model2 = [[FillExpressModel alloc] init];
    model2.topStr = @"到仓自提";
    model2.detailStr = @"免运费";
    model2.isShow = NO;
    NSMutableArray *arr = @[model1,model2].mutableCopy;
    return arr;
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        _dataArr = [self makeDistributionData];
    }
    return _dataArr;
}

@end
